package com.gandong8.trace.web.domain.common;

import lombok.Data;

import java.io.Serializable;

@Data
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 2710211206433359202L;
    private int code;
    private String msg;
    private T data;

    protected Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> Result<T> run(boolean result) {
        return result? success():failed();
    }

    public static <T> Result<T> success() {
        return new Result<T>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), null);
    }

    public static <T> Result<T> success(T data) {
        return new Result<T>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), data);
    }

    public static <T> Result<T> failed() {
        return new Result<T>(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMsg(), null);
    }

    public static <T> Result<T> failed(ResultCode code) {
        return new Result<T>(code.getCode(), code.getMsg(), null);
    }

    public static <T> Result<T> failed(String msg) {
        return new Result<T>(ResultCode.FAILED.getCode(), msg, null);
    }
}
